package com.qili.controller.editor;
import com.qili.base.entity.CurrentUser;
import com.qili.service.supervisor.SupervisorFileService;
import com.qili.util.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("ueditor")
public class UeditorController {

    @Autowired
    SupervisorFileService supervisorFileService;

    @Autowired
    UploadUtil uploadUtil;

    /**
     * 上传配置：即不走config.json，模拟config.json里的内容，解决后端配置项不正确，无法上传的问题
     *
     * @return
     */
    @RequestMapping("config")
    @ResponseBody
    public String uploadConfig() {
        String s = "{\n" +
                "                \"imageActionName\": \"uploadImage\",\n" +
                "                \"imageFieldName\": \"file\", \n" +
                "                \"imageMaxSize\": 20480000, \n" +
                "                \"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"], \n" +
                "                \"imageCompressEnable\": true, \n" +
                "                \"imageCompressBorder\": 1600, \n" +
                "                \"imageInsertAlign\": \"none\", \n" +
                "                \"imageUrlPrefix\": \"\",\n" +
                "                \"imagePathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\"," +





                "                \"fileActionName\": \"uploadFile\", \n" +
                "                \"fileFieldName\": \"upfile\", \n" +
                "                \"filePathFormat\": \"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
                "                \"fileUrlPrefix\": \"\", \n" +
                "                \"fileMaxSize\": 51200000, \n" +
                "                \"fileAllowFiles\": [\n" +
                "                    \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\",\n" +
                "                    \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
                "                    \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\",\n" +
                "                    \".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\",\n" +
                "                    \".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"\n" +
                "                  ]}";
        return s;
    }

    /**
     * 编辑器上传图片
     * @param filedata
     * @return
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addFile(@RequestParam(value = "file", required = false) MultipartFile filedata) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 当前用户信息
        Subject sub = SecurityUtils.getSubject();
        CurrentUser currentUser = (CurrentUser) sub.getPrincipal();
        JsonUtil re = new JsonUtil();
        try {
            LoadType loadType = uploadUtil.upload(filedata, UploadType.ArtImage);
            re.setMsg(loadType.getOriginalFileName());
            //这几个值很关键
            map.put("state", "SUCCESS");
            //这里是图片完整路径，不管是本地路径，还是文件服务器路径都行
            map.put("url",loadType.getArtImagePath());
            map.put("original",loadType.getOriginalFileName());
            map.put("title",loadType.getOriginalFileName());
            return map;
        } catch (Exception e) {
            re.setMsg("保存失败");
            re.setFlag(false);
            e.printStackTrace();
            return null;
        }
    }
}
